var util = require('../../utils/util.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    disable: true,
    showView: true,
    interval: 3000,
    duration: 1000,
    buttonClicked: false,  //点击事件
  },
  goodsCarNumber: 0,
  /**
   * 查看更多
   */
  moreCheck:function(){
    wx.navigateTo({
      url: '../commodityClassify/commodityClassify',
    })
  },
  /**
   * 活动一
   */
  acticeOne:function(e){
    const id = e.currentTarget.dataset.actid
    console.log(e.currentTarget.dataset.actid)
    if(id == 1){
        wx.navigateTo({
        url: '../todayNew/todayNew',
      })
    }else if(id == 2){
      wx.navigateTo({
        url: '../everyNew/everyNew',
      })
    }else if(id == 3){
      wx.navigateTo({
        url: '../activePage/activePage',
      })
    }
  },
  /**
   * 点击事件猜你喜欢--商品列表、购物车
   */
  commodity:function(e){
    const goodsId = e.currentTarget.dataset.goodsid
    if (!this.data.buttonClicked) {
      util.buttonClicked(this);
      const goodsId = e.currentTarget.dataset.goodsid;
      wx.navigateTo({
        url: '../commodityDetail/commodityDetail?goodsId=' + goodsId
      })
    }
  },
  getInCar:function(e){
    var that = this;
    const goodsId = e.currentTarget.dataset.goodsid;
    const goodsName = e.currentTarget.dataset.goodsname;
    const shopPrice = e.currentTarget.dataset.shopprice;
    const originalImg = e.currentTarget.dataset.originalimg;
   
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'insertGoodsInCartShop',
      data: {
            goodsNumber: 1,
            goodsPic:  originalImg,
            goodsName: goodsName,
            goodsPrice: shopPrice,
            goodsId: goodsId,
            cartType: 2,
            userId: userId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function(res) {
        wx.showToast({
          title: '购物车添加成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 一级分类
   */
  detailItem:function(e){
    console.log(e)
    const index = e.currentTarget.dataset.index
    const catId = e.currentTarget.dataset.catid
    wx.navigateTo({
      url: '../businessItem/businessItem?parentId=' + catId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    /**
     * 轮播图
     */
    const that =  this;
    wx.request({
      url: app.globalData.url +'getShopBannerInFront',
      data: {
        shopBannerType:1
      },
      method: 'GET',
      header: {
        "Content-Type": "application/json"
      },
      success: function(res) {
        that.setData({
          banner: res.data.object 
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    /**
     * 活动
     */
    wx.request({
      url: app.globalData.url + 'getTodaysPromoteName',
      data: '',
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: function(res) {
        that.setData({
          actives:res.data.object
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })

    /**查询购物车列表 */
    that.queryCarList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.request({
      url: app.globalData.url+'getAllFristCategory',
      data: '',
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        that.setData({
          catName: res.data.object,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    /**
     * 活动--猜你喜欢
     */
    wx.request({
      url: app.globalData.url + 'getTodaysPromoteGoods',
      data: {
        actType: 4
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          goods: res.data.object
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * banner轮播图
   */
  activePage:function(e){
    console.log(e.currentTarget.dataset.bannerid)
    const bannerid = e.currentTarget.dataset.bannerid
    wx.navigateTo({
      url: '../bannerActive/bannerActive?bannerId=' + bannerid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**首页购物车跳转 */
  InshoppingCar: function () {
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  onShow(){
    this.queryCarList();
  },
  /**查询购物车列表 */
  queryCarList:function(){
    const that = this;
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'getShopCart',
      data: {
        userId: userId,
        cartType: 2,
        pageIndex: 1,
        pageSize: 10
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.goodsCarNumber = 0
        if (res.data.object.cartList != undefined) {

          for (let i = 0; i < res.data.object.cartList.length; i++) {
            that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
          }
          that.setData({
            goodsCarNumber: that.goodsCarNumber
          })
        } else {
          that.setData({
            goodsCarNumber: 0
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})